<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>com.myjavatools.web.ClientHttpRequest.java</title>
</head>
<body>
<pre class="sourcecode"><code>
<b>package</b> com.myjavatools.web;<br><br><b>import</b> java.net.URLConnection;<br><b>import</b> java.net.URL;<br><b>import</b> java.io.IOException;<br><b>import</b> java.util.HashMap;<br><b>import</b> java.util.Map;<br><b>import</b> java.io.File;<br><b>import</b> java.io.InputStream;<br><b>import</b> java.util.Random;<br><b>import</b> java.io.OutputStream;<br><b>import</b> java.io.FileInputStream;<br><b>import</b> java.util.Iterator;<br><br><font
 color="#003399"><i>/**<br> * &lt;p&gt;Title: Client HTTP Request class&lt;/p&gt;<br> * &lt;p&gt;Description: this class helps to send POST HTTP requests with various form data,<br> * including files. Cookies can be added to be included in the request.&lt;/p&gt;<br> *<br> * @author Vlad Patryshev<br> * @version 1.0<br> */</i></font>
<b>public</b> <b>class</b> ClientHttpRequest {<br>  URLConnection connection;<br>  OutputStream os = <b>null</b>;<br>  Map cookies = <b>new</b> HashMap();<br><br>  <b>protected</b> <b>void</b> connect() <b>throws</b> IOException {<br>    <b>if</b> (os == <b>null</b>) os = connection.getOutputStream();<br>  }<br><br>  <b>protected</b> <b>void</b> write(<b>char</b> c) <b>throws</b> IOException {<br>    connect();<br>    os.write(c);<br>  }<br><br>  <b>protected</b> <b>void</b> write(String s) <b>throws</b> IOException {<br>    connect();<br>    os.write(s.getBytes());<br>  }<br><br>  <b>protected</b> <b>void</b> newline() <b>throws</b> IOException {<br>    connect();<br>    write(<font
 color="#9933cc">"\r\n"</font>);<br>  }<br><br>  <b>protected</b> <b>void</b> writeln(String s) <b>throws</b> IOException {<br>    connect();<br>    write(s);<br>    newline();<br>  }<br><br>  <b>private</b> <b>static</b> Random random = <b>new</b> Random();<br><br>  <b>protected</b> <b>static</b> String randomString() {<br>    <b>return</b> <b>Long</b>.toString(random.nextLong(), 36);<br>  }<br><br>  String boundary = <font
 color="#9933cc">"---------------------------"</font> + randomString() + randomString() + randomString();<br><br>  <b>private</b> <b>void</b> boundary() <b>throws</b> IOException {<br>    write(<font
 color="#9933cc">"--"</font>);<br>    write(boundary);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * Creates a new multipart POST HTTP request on a freshly opened URLConnection<br>   *<br>   * @param connection an already open URL connection<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> ClientHttpRequest(URLConnection connection) <b>throws</b> IOException {<br>    <b>this</b>.connection = connection;<br>    connection.setDoOutput(<b>true</b>);<br>    connection.setRequestProperty(<font
 color="#9933cc">"Content-Type"</font>,<br>                                  <font
 color="#9933cc">"multipart/form-data; boundary="</font> + boundary);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * Creates a new multipart POST HTTP request for a specified URL<br>   *<br>   * @param url the URL to send request to<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> ClientHttpRequest(URL url) <b>throws</b> IOException {<br>    <b>this</b>(url.openConnection());<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * Creates a new multipart POST HTTP request for a specified URL string<br>   *<br>   * @param urlString the string representation of the URL to send request to<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> ClientHttpRequest(String urlString) <b>throws</b> IOException {<br>    <b>this</b>(<b>new</b> URL(urlString));<br>  }<br><br><br>  <b>private</b> <b>void</b> postCookies() {<br>    StringBuffer cookieList = <b>new</b> StringBuffer();<br><br>    <b>for</b> (Iterator i = cookies.entrySet().iterator(); i.hasNext();) {<br>      Map.Entry entry = (Map.Entry)(i.next());<br>      cookieList.append(entry.getKey().toString() + <font
 color="#9933cc">"="</font> + entry.getValue());<br><br>      <b>if</b> (i.hasNext()) {<br>        cookieList.append(<font
 color="#9933cc">"; "</font>);<br>      }<br>    }<br>    <b>if</b> (cookieList.length() &gt; 0) {<br>      connection.setRequestProperty(<font
 color="#9933cc">"Cookie"</font>, cookieList.toString());<br>    }<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * adds a cookie to the requst<br>   * @param name cookie name<br>   * @param value cookie value<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> <b>void</b> setCookie(String name, String value) <b>throws</b> IOException {<br>    cookies.put(name, value);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * adds cookies to the request<br>   * @param cookies the cookie "name-to-value" map<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> <b>void</b> setCookies(Map cookies) <b>throws</b> IOException {<br>    <b>if</b> (cookies == <b>null</b>) <b>return</b>;<br>    <b>this</b>.cookies.putAll(cookies);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * adds cookies to the request<br>   * @param cookies array of cookie names and values (cookies[2*i] is a name, cookies[2*i + 1] is a value)<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> <b>void</b> setCookies(String[] cookies) <b>throws</b> IOException {<br>    <b>if</b> (cookies == <b>null</b>) <b>return</b>;<br>    <b>for</b> (<b>int</b> i = 0; i &lt; cookies.length - 1; i+=2) {<br>      setCookie(cookies[i], cookies[i+1]);<br>    }<br>  }<br><br>  <b>private</b> <b>void</b> writeName(String name) <b>throws</b> IOException {<br>    newline();<br>    write(<font
 color="#9933cc">"Content-Disposition: form-data; name=\"</font><font
 color="#9933cc">");</font>
    write(name);
    write(<font
 color="#9933cc">'"'</font>);<br>  }<br><br>  <font color="#003399"><i>/**<br>   * adds a string parameter to the request<br>   * @param name parameter name<br>   * @param value parameter value<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> <b>void</b> setParameter(String name, String value) <b>throws</b> IOException {<br>    boundary();<br>    writeName(name);<br>    newline(); newline();<br>    writeln(value);<br>  }<br><br>  <b>private</b> <b>static</b> <b>void</b> pipe(InputStream in, OutputStream out) <b>throws</b> IOException {<br>    <b>byte</b>[] buf = <b>new</b> <b>byte</b>[500000];<br>    <b>int</b> nread;<br>    <b>int</b> navailable;<br>    <b>int</b> total = 0;<br>    <b>synchronized</b> (in) {<br>      <b>while</b>((nread = in.read(buf, 0, buf.length)) &gt;= 0) {<br>        out.write(buf, 0, nread);<br>        total += nread;<br>      }<br>    }<br>    out.flush();<br>    in.close();<br>    buf = <b>null</b>;<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * adds a file parameter to the request<br>   * @param name parameter name<br>   * @param filename the name of the file<br>   * @param is input stream to read the contents of the file from<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> <b>void</b> setParameter(String name, String filename, InputStream is) <b>throws</b> IOException {<br>    boundary();<br>    writeName(name);<br>    write(<font
 color="#9933cc">"; filename=\"</font><font color="#9933cc">");</font>
    write(filename);
    write(<font
 color="#9933cc">'"'</font>);<br>    newline();<br>    write(<font
 color="#9933cc">"Content-Type: "</font>);<br>    String type = connection.guessContentTypeFromName(filename);<br>    <b>if</b> (type == <b>null</b>) type = <font
 color="#9933cc">"application/octet-stream"</font>;<br>    writeln(type);<br>    newline();<br>    pipe(is, os);<br>    newline();<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * adds a file parameter to the request<br>   * @param name parameter name<br>   * @param file the file to upload<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> <b>void</b> setParameter(String name, File file) <b>throws</b> IOException {<br>    setParameter(name, file.getPath(), <b>new</b> FileInputStream(file));<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * adds a parameter to the request; if the parameter is a File, the file is uploaded, otherwise the string value of the parameter is passed in the request<br>   * @param name parameter name<br>   * @param object parameter value, a File or anything else that can be stringified<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> <b>void</b> setParameter(String name, Object object) <b>throws</b> IOException {<br>    <b>if</b> (object <b>instanceof</b> File) {<br>      setParameter(name, (File) object);<br>    } <b>else</b> {<br>      setParameter(name, object.toString());<br>    }<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * adds parameters to the request<br>   * @param parameters "name-to-value" map of parameters; if a value is a file, the file is uploaded, otherwise it is stringified and sent in the request<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> <b>void</b> setParameters(Map parameters) <b>throws</b> IOException {<br>    <b>if</b> (parameters == <b>null</b>) <b>return</b>;<br>    <b>for</b> (Iterator i = parameters.entrySet().iterator(); i.hasNext();) {<br>      Map.Entry entry = (Map.Entry)i.next();<br>      setParameter(entry.getKey().toString(), entry.getValue());<br>    }<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * adds parameters to the request<br>   * @param parameters array of parameter names and values (parameters[2*i] is a name, parameters[2*i + 1] is a value); if a value is a file, the file is uploaded, otherwise it is stringified and sent in the request<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> <b>void</b> setParameters(Object[] parameters) <b>throws</b> IOException {<br>    <b>if</b> (parameters == <b>null</b>) <b>return</b>;<br>    <b>for</b> (<b>int</b> i = 0; i &lt; parameters.length - 1; i+=2) {<br>      setParameter(parameters[i].toString(), parameters[i+1]);<br>    }<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * posts the requests to the server, with all the cookies and parameters that were added<br>   * @return input stream with the server response<br>   * @throws IOException<br>   */</i></font>
  <b>public</b> InputStream post() <b>throws</b> IOException {<br>    boundary();<br>    writeln(<font
 color="#9933cc">"--"</font>);<br>    os.close();<br>    <b>return</b> connection.getInputStream();<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with parameters that are passed in the argument<br>   * @param parameters request parameters<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameters<br>   */</i></font>
  <b>public</b> InputStream post(Map parameters) <b>throws</b> IOException {<br>    setParameters(parameters);<br>    <b>return</b> post();<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with parameters that are passed in the argument<br>   * @param parameters request parameters<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameters<br>   */</i></font>
  <b>public</b> InputStream post(Object[] parameters) <b>throws</b> IOException {<br>    setParameters(parameters);<br>    <b>return</b> post();<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with cookies and parameters that are passed in the arguments<br>   * @param cookies request cookies<br>   * @param parameters request parameters<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameters<br>   * @see setCookies<br>   */</i></font>
  <b>public</b> InputStream post(Map cookies, Map parameters) <b>throws</b> IOException {<br>    setCookies(cookies);<br>    setParameters(parameters);<br>    <b>return</b> post();<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * posts the requests to the server, with all the cookies and parameters that were added before (if any), and with cookies and parameters that are passed in the arguments<br>   * @param cookies request cookies<br>   * @param parameters request parameters<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameters<br>   * @see setCookies<br>   */</i></font>
  <b>public</b> InputStream post(String[] cookies, Object[] parameters) <b>throws</b> IOException {<br>    setCookies(cookies);<br>    setParameters(parameters);<br>    <b>return</b> post();<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * post the POST request to the server, with the specified parameter<br>   * @param name parameter name<br>   * @param value parameter value<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameter<br>   */</i></font>
  <b>public</b> InputStream post(String name, Object value) <b>throws</b> IOException {<br>    setParameter(name, value);<br>    <b>return</b> post();<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * post the POST request to the server, with the specified parameters<br>   * @param name1 first parameter name<br>   * @param value1 first parameter value<br>   * @param name2 second parameter name<br>   * @param value2 second parameter value<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameter<br>   */</i></font>
  <b>public</b> InputStream post(String name1, Object value1, String name2, Object value2) <b>throws</b> IOException {<br>    setParameter(name1, value1);<br>    <b>return</b> post(name2, value2);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * post the POST request to the server, with the specified parameters<br>   * @param name1 first parameter name<br>   * @param value1 first parameter value<br>   * @param name2 second parameter name<br>   * @param value2 second parameter value<br>   * @param name3 third parameter name<br>   * @param value3 third parameter value<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameter<br>   */</i></font>
  <b>public</b> InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3) <b>throws</b> IOException {<br>    setParameter(name1, value1);<br>    <b>return</b> post(name2, value2, name3, value3);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * post the POST request to the server, with the specified parameters<br>   * @param name1 first parameter name<br>   * @param value1 first parameter value<br>   * @param name2 second parameter name<br>   * @param value2 second parameter value<br>   * @param name3 third parameter name<br>   * @param value3 third parameter value<br>   * @param name4 fourth parameter name<br>   * @param value4 fourth parameter value<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameter<br>   */</i></font>
  <b>public</b> InputStream post(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) <b>throws</b> IOException {<br>    setParameter(name1, value1);<br>    <b>return</b> post(name2, value2, name3, value3, name4, value4);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * posts a new request to specified URL, with parameters that are passed in the argument<br>   * @param parameters request parameters<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameters<br>   */</i></font>
  <b>public</b> <b>static</b> InputStream post(URL url, Map parameters) <b>throws</b> IOException {<br>    <b>return</b> <b>new</b> ClientHttpRequest(url).post(parameters);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * posts a new request to specified URL, with parameters that are passed in the argument<br>   * @param parameters request parameters<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameters<br>   */</i></font>
  <b>public</b> <b>static</b> InputStream post(URL url, Object[] parameters) <b>throws</b> IOException {<br>    <b>return</b> <b>new</b> ClientHttpRequest(url).post(parameters);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * posts a new request to specified URL, with cookies and parameters that are passed in the argument<br>   * @param cookies request cookies<br>   * @param parameters request parameters<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setCookies<br>   * @see setParameters<br>   */</i></font>
  <b>public</b> <b>static</b> InputStream post(URL url, Map cookies, Map parameters) <b>throws</b> IOException {<br>    <b>return</b> <b>new</b> ClientHttpRequest(url).post(cookies, parameters);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * posts a new request to specified URL, with cookies and parameters that are passed in the argument<br>   * @param cookies request cookies<br>   * @param parameters request parameters<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setCookies<br>   * @see setParameters<br>   */</i></font>
  <b>public</b> <b>static</b> InputStream post(URL url, String[] cookies, Object[] parameters) <b>throws</b> IOException {<br>    <b>return</b> <b>new</b> ClientHttpRequest(url).post(cookies, parameters);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * post the POST request specified URL, with the specified parameter<br>   * @param name parameter name<br>   * @param value parameter value<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameter<br>   */</i></font>
  <b>public</b> <b>static</b> InputStream post(URL url, String name1, Object value1) <b>throws</b> IOException {<br>    <b>return</b> <b>new</b> ClientHttpRequest(url).post(name1, value1);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * post the POST request to specified URL, with the specified parameters<br>   * @param name1 first parameter name<br>   * @param value1 first parameter value<br>   * @param name2 second parameter name<br>   * @param value2 second parameter value<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameter<br>   */</i></font>
  <b>public</b> <b>static</b> InputStream post(URL url, String name1, Object value1, String name2, Object value2) <b>throws</b> IOException {<br>    <b>return</b> <b>new</b> ClientHttpRequest(url).post(name1, value1, name2, value2);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * post the POST request to specified URL, with the specified parameters<br>   * @param name1 first parameter name<br>   * @param value1 first parameter value<br>   * @param name2 second parameter name<br>   * @param value2 second parameter value<br>   * @param name3 third parameter name<br>   * @param value3 third parameter value<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameter<br>   */</i></font>
  <b>public</b> <b>static</b> InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3) <b>throws</b> IOException {<br>    <b>return</b> <b>new</b> ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3);<br>  }<br><br>  <font
 color="#003399"><i>/**<br>   * post the POST request to specified URL, with the specified parameters<br>   * @param name1 first parameter name<br>   * @param value1 first parameter value<br>   * @param name2 second parameter name<br>   * @param value2 second parameter value<br>   * @param name3 third parameter name<br>   * @param value3 third parameter value<br>   * @param name4 fourth parameter name<br>   * @param value4 fourth parameter value<br>   * @return input stream with the server response<br>   * @throws IOException<br>   * @see setParameter<br>   */</i></font>
  <b>public</b> <b>static</b> InputStream post(URL url, String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) <b>throws</b> IOException {<br>    <b>return</b> <b>new</b> ClientHttpRequest(url).post(name1, value1, name2, value2, name3, value3, name4, value4);<br>  }<br>}<br><br></code></pre>
</body>
</html>
